home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Updaters / AppMaker 1.5.2->1.5.4 / Libraries / THINK / AMLibraryC / AEvent.c next >
Encoding:
C/C++ Source or Header  |  1993-05-26  |  5.3 KB  |  212 lines  |  [TEXT/KAHL]

  1. /* © 1991, Bowers Development Corp.  */
  2. /* AEvent.c */
  3.  
  4. #include <Types.h>
  5. #include <Quickdraw.h>
  6. #include <Controls.h>
  7. #include <Dialogs.h>
  8. #include <Events.h>
  9. #include <Files.h>
  10. #include <Lists.h>
  11. #include <Menus.h>
  12. #include <TextEdit.h>
  13. #include <AppleEvents.h>
  14.  
  15. #include "Globals.h"    
  16. #include "ResourceDefs.h"    
  17. #include "Miscellany.h"    
  18. #include "FileM.h"    
  19. #include "AEvent.h"    
  20.  
  21. #pragma segment AEvent
  22.  
  23. /*----------*/
  24. void    InitializeAE ()
  25. {
  26.     OSErr            errCode;
  27.  
  28.     errCode = AEInstallEventHandler (kCoreEventClass, kAEOpenApplication, (EventHandlerProcPtr) DoAEOpenApp, 0, false);
  29.     errCode = AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, (EventHandlerProcPtr) DoAEOpenDoc, 0, false);
  30.     errCode = AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments, (EventHandlerProcPtr) DoAEPrintDoc, 0, false);
  31.     errCode = AEInstallEventHandler (kCoreEventClass, kAEQuitApplication, (EventHandlerProcPtr) DoAEQuit, 0, false);
  32.  
  33. } /*InitializeAE*/
  34.  
  35. /*----------*/
  36. void    DoHighLevelEvent ()
  37. {
  38.     OSErr            errCode;
  39.  
  40. /*  check to see if the Apple event is of a user-defined class;  */
  41. /*  if so, handle it. Otherwise, call AEProcessAppleEvent.        */
  42.  
  43.     errCode = AEProcessAppleEvent (&curEvent);
  44.     if (errCode == errAEEventNotHandled) {
  45. /*??        Acknowledge (CantHandleAEVTID);        ??*/
  46.     }
  47.  
  48. } /*DoHighLevelEvent*/
  49.  
  50. /*----------*/
  51. /* This routine checks to verify that the caller has processed */
  52. /* all of the event's parameters. If no parameters are found */
  53. /* then it returns noErr. */
  54. /*----------*/
  55. OSErr    MyGotRequiredParams    (const AppleEvent        *theEvent)
  56. {
  57.     OSErr            errCode;
  58.     Size            actualSize;
  59.     DescType        returnedType;
  60.  
  61.     errCode = AEGetAttributePtr (theEvent, keyMissedKeywordAttr, typeWildCard,
  62.                                     &returnedType, NULL, 0, &actualSize);
  63.  
  64.     if (errCode == errAEDescNotFound) {        /*no parameters => no error*/
  65.         errCode = noErr;
  66.     } else if (errCode == noErr) {            /*got a parameter => it wasn't handled*/
  67.         errCode = errAEEventNotHandled;
  68.     }                                        /*AEGetAttributePtr failed*/
  69.  
  70.     return (errCode);
  71. } /*MyGotRequiredParams*/
  72.  
  73. /*----------*/
  74. pascal OSErr    DoAEOpenApp        (AppleEvent        *theEvent,
  75.                                  AppleEvent        *reply,
  76.                                  long            refCon)
  77. {
  78. #pragma unused (reply)
  79. #pragma unused (refCon)
  80.  
  81.     OSErr            errCode;
  82.  
  83.     errCode = MyGotRequiredParams (theEvent);
  84.     if (errCode == noErr) {
  85.         OpenApp ();
  86.     }
  87.  
  88.     return (errCode);
  89. } /*DoAEOpenApp*/
  90.  
  91. #if !defined(applec) /* MPW C */ && !defined(THINK_C)    /* AUX */
  92. /*----------*/
  93. /* Need to use PBOpenWD here instead of OpenWD because of an AUX 3.0 bug which omitted */
  94. /* the glue for OpenWD, resulting in a link error if OpenWD is used. */
  95. /*----------*/
  96. static OSErr AUXOpenWD            (short            vRefNum,
  97.                                  long            dirID,
  98.                                  long            procID,
  99.                                  short            *wdRefNum)
  100. {
  101.     WDPBRec            wdOpenRec;
  102.     OSErr            errCode;
  103.     short            i;
  104.     Ptr                p;
  105.  
  106. /* Need to use a local WDPBRec here and clear it out ourselves (instead of using a WDPBPtr */
  107. /* and NewPtrClear) because of an AUX 3.0 bug which omitted the glue for NewPtrClear, */
  108. /* resulting in a link error if NewPtrClear is used. */
  109.     for (i = 0, p = (Ptr) &wdOpenRec; i < sizeof (WDPBRec); i++, p++) {
  110.         *p = 0;            /* clear out wdOpenRec */
  111.     }
  112.     wdOpenRec.ioVRefNum  = vRefNum;
  113.     wdOpenRec.ioWDProcID = procID;
  114.     wdOpenRec.ioWDDirID  = dirID;
  115.     errCode = PBOpenWD (&wdOpenRec, false);
  116.     if (errCode == noErr) {
  117.         *wdRefNum = wdOpenRec.ioVRefNum;
  118.     }
  119.  
  120.     return (errCode);
  121. } /*AUXOpenWD*/
  122. #endif /* AUX */
  123.  
  124. /*----------*/
  125. pascal OSErr    DoAEOpenDoc        (AppleEvent        *theEvent,
  126.                                  AppleEvent        *reply,
  127.                                  long            refCon)
  128. {
  129. #pragma unused (reply)
  130. #pragma unused (refCon)
  131.  
  132.     OSErr            errCode;
  133.     OSErr            ignoreErr;
  134.     AEDescList        docList;
  135.     long            itemsInList;
  136.     long            index;
  137.     AEKeyword        keyword;
  138.     DescType        returnedType;
  139.     Size            actualSize;
  140.     FSSpec            myFSS;
  141.     short            wdRefNum;
  142.  
  143.     errCode = AEGetParamDesc (theEvent, keyDirectObject, typeAEList, &docList);
  144.     if (errCode == noErr) {
  145.         errCode = MyGotRequiredParams (theEvent);
  146.         if (errCode == noErr) {
  147.             errCode = AECountItems (&docList, &itemsInList);
  148.             if (errCode == noErr) {
  149.                 for (index = 1; index <= itemsInList; index++) {
  150.                     errCode = AEGetNthPtr (&docList, index, typeFSS, &keyword,
  151.                             &returnedType, (Ptr) &myFSS, sizeof (myFSS), &actualSize);
  152.                     if (errCode == noErr) {
  153. #if defined(applec) /* MPW C */ || defined(THINK_C)
  154.                         errCode = OpenWD (myFSS.vRefNum, myFSS.parID, 0L /* procID */, &wdRefNum);
  155. #else    /* AUX */
  156.                         errCode = AUXOpenWD (myFSS.vRefNum, myFSS.parID, 0L /* procID */, &wdRefNum);
  157. #endif
  158.                         if (errCode == noErr) {
  159.                             OpenDoc (myFSS.name, wdRefNum);
  160.                         }
  161.                     }
  162.                 } /*for*/
  163.             }
  164.         }
  165.         ignoreErr = AEDisposeDesc (&docList);
  166.     }
  167.  
  168.     return (errCode);
  169. } /*DoAEOpenDoc*/
  170.  
  171. /*----------*/
  172. pascal OSErr    DoAEPrintDoc    (AppleEvent        *theEvent,
  173.                                  AppleEvent        *reply,
  174.                                  long            refCon)
  175. {
  176. #pragma unused (reply)
  177. #pragma unused (refCon)
  178.  
  179.     OSErr            errCode;
  180.  
  181.     errCode = MyGotRequiredParams (theEvent);
  182.     if (errCode == noErr) {
  183. /*??        Acknowledge (CantPrintID);    ??*/
  184.     }
  185.  
  186.     return (errCode);
  187. } /*DoAEPrintDoc*/
  188.  
  189. /*----------*/
  190. pascal OSErr    DoAEQuit        (AppleEvent        *theEvent,
  191.                                  AppleEvent        *reply,
  192.                                  long            refCon)
  193. {
  194. #pragma unused (reply)
  195. #pragma unused (refCon)
  196.  
  197.     OSErr            errCode;
  198.  
  199.     errCode = MyGotRequiredParams (theEvent);
  200.     if (errCode == noErr) {
  201.         DoQuit ();
  202.     }
  203.  
  204.     return (errCode);
  205. } /*DoAEQuit*/
  206.  
  207. /*----------*/
  208. /* Add an InteractWithUser as an example of how it is done.*/
  209. /*    Include an IdleProc which updates windows in the background.*/
  210.  
  211. /*AEvent*/
  212.